/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Juego;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JLabel;

/**
 *
 * @author Eddison
 */
public class Juego extends javax.swing.JFrame {
    ArrayList <HiloDisparo> listadisparos= new ArrayList <HiloDisparo>();
    ArrayList <HiloAsteroide> listaasteroides= new ArrayList <HiloAsteroide>();
    
    
    public Juego() {
       
        initComponents();
        xxx.setVisible(false);
        bala.setVisible(false);
        nave.setVisible(false);
        jLabel3.setVisible(false);
          this.JP.validate();
         this.JP.updateUI();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        nave = new javax.swing.JLabel();
        xxx = new javax.swing.JLabel();
        bala = new javax.swing.JLabel();
        JP = new org.edisoncor.gui.panel.PanelImage();
        nave1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(" Star Trek beta");
        setPreferredSize(new java.awt.Dimension(600, 600));
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jPanel1.setPreferredSize(new java.awt.Dimension(450, 450));
        jPanel1.setLayout(null);

        nave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/nave2.gif"))); // NOI18N
        jPanel1.add(nave);
        nave.setBounds(149, 364, 75, 75);

        xxx.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Nave.gif"))); // NOI18N
        jPanel1.add(xxx);
        xxx.setBounds(22, 11, 50, 50);

        bala.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/bala_1.gif"))); // NOI18N
        jPanel1.add(bala);
        bala.setBounds(180, 350, 10, 10);

        JP.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/space.jpg"))); // NOI18N

        nave1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                nave1KeyPressed(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/LOSESS.jpg"))); // NOI18N
        jLabel3.setText("jLabel3");

        javax.swing.GroupLayout JPLayout = new javax.swing.GroupLayout(JP);
        JP.setLayout(JPLayout);
        JPLayout.setHorizontalGroup(
            JPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JPLayout.createSequentialGroup()
                .addGap(327, 327, 327)
                .addComponent(nave1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        JPLayout.setVerticalGroup(
            JPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, JPLayout.createSequentialGroup()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 449, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(nave1)
                .addGap(158, 158, 158))
        );

        jPanel1.add(JP);
        JP.setBounds(-2, 0, 450, 450);

        jLabel1.setText("Time");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/play1.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(173, 173, 173)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(283, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(57, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
            this.nave.setVisible(true);
            this.jLabel3.setVisible(false);
            this.jButton1.addKeyListener(new KeyListener(){

            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent evt) {
                int x=nave.getX();
                int y=nave.getY();
               // if(y<=369 || x<=-1){
                         System.out.println(x+" "+" "+y);
                switch(evt.getKeyCode()){
                    case KeyEvent.VK_RIGHT:
                        x=x+5;
                        nave.setLocation(x, y);
                        break;
                    case KeyEvent.VK_LEFT:
                        x=x-5;
                        nave.setLocation(x, y);
                       break;
                    case KeyEvent.VK_UP:
                        y=y-5;
                        nave.setLocation(x, y);
                        break;
                    case KeyEvent.VK_DOWN:
                        y=y+5;
                        nave.setLocation(x, y);
                           break;
                    case KeyEvent.VK_A:
                        Disparar();
                        break;
                } 
                }
               
       //     }

            @Override
            public void keyReleased(KeyEvent e) {
    
            }
        
    });

        Asteroides();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void nave1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nave1KeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_nave1KeyPressed

    public void Disparar(){
        JLabel bala1=new JLabel();
        bala1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/bala_1.gif")));
        bala1.setBounds((nave.getX()), nave.getY(), 16, 16);
        this.JP.add(bala1);
        HiloDisparo disparo=new HiloDisparo(bala1,nave.getX(),nave.getY(),listaasteroides);
        //listadisparos.add(disparo);
        listadisparos.add(disparo);
        disparo.start();
        
    }
    
    private void Asteroides(){
        for(int i=0;i<5;i++){
           int nivel=1;
           JLabel asteroide=new JLabel();
           asteroide.setBounds(aleatorio(),0,45,45);
           asteroide.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Nave.gif")));
         
         //  perdio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/boom.jpg")));
           this.JP.add(asteroide);
           HiloAsteroide h=new HiloAsteroide(asteroide,aleatorio(),0,nave,listadisparos,JP,jLabel3);
           listaasteroides.add(h);
           h.start();
        }
    }
    
    
    
  
    
    
    
    
    
  
    
    private void mover(java.awt.event.KeyEvent evt){
        int x=nave.getX();
        int y=nave.getY();
         
        if(x>600)
        {
           System.out.println(x+y);
        switch(evt.getKeyCode()){
            case KeyEvent.VK_RIGHT:
                x=x+2;
                nave.setLocation(x, y);
                break;
            case KeyEvent.VK_LEFT:
                x=x-2;
                nave.setLocation(x, y);
               break;
            case KeyEvent.VK_UP:
                y=y-2;
                nave.setLocation(x, y);
                break;
            case KeyEvent.VK_DOWN:
                y=y+2;
                nave.setLocation(x, y);
                   break;
        } 
        }
    }
    
    private static int aleatorio() {
        double retornoMath;
        retornoMath = Math.random();
        return((int)( retornoMath * 400 ) );
    }
    
    public void perder(){
    jLabel3.setVisible(true);
   JP.setVisible(false);
    }

  
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Juego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Juego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Juego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Juego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Juego().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.edisoncor.gui.panel.PanelImage JP;
    private javax.swing.JLabel bala;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel nave;
    private javax.swing.JLabel nave1;
    private javax.swing.JLabel xxx;
    // End of variables declaration//GEN-END:variables
}
